﻿using NLog;
using System;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Syncfusion.Windows.Forms;
using Syncfusion.Windows.Forms.Tools;

namespace PMngr.Main
{
    public partial class MainForm : Office2007Form
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        [Dependency("MUnits")]
        public MstUnits MUnitControl { get; set; }
        [Dependency("SUnits")]
        public SubUnits SUnitControl { get; set; }
        [Dependency("Options")]
        public Options OptionControl { get; set; }
        [Dependency("SActions")]
        public Single SingleControl { get; set; }
        [Dependency("WActions")]
        public Whole WholeControl { get; set; }
        [Dependency("SuperReport")]
        public SuperReport SuperReportControl { get; set; }
        [Dependency("SubReport")]
        public SubReport SubReportControl { get; set; }
        [Dependency("ProfileUpdate")]
        public ProfileUpdate ProfileControl { get; set; }
        [Dependency("PieChart")]
        public PieChart PieChartControl { get; set; }
        [Dependency("SingleApproval")]
        public SingleApproval SingleApproval { get; set; }
        [Dependency("WholeApproval")]
        public WholeApproval WholeApproval { get; set; }
        [Dependency("WholeTemplate")]
        public WholeTemplate WholeTemplate { get; set; }
        [Dependency("TransferReport")]
        public TransferReport TransferReport { get; set; }
        [Dependency("SystemBackup")]
        public SystemBackup SystemBackup { get; set; }
        [Dependency("UnitImporter")]
        public UnitImporter UnitImporter { get; set; }
        [Dependency("UnitTemplate")]
        public UnitTemplate UnitTemplate { get; set; }

        public MainForm()
        {
            InitializeComponent();
            changePassword.LogWriter = new LoggingWriter(LogMessage);
            // display the login form
            var loginForm = new LoginForm();
            DialogResult result = loginForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                var principal = System.Threading.Thread.CurrentPrincipal;
                LogMessage(String.Format(Utilities.LoginPrompt, principal.Identity.Name));
                statusUser.Text = principal.Identity.Name;
            }
            else
                ApplicationExit.ExitApplication();
        }

        private void FileMenuClick(object sender, EventArgs e)
        {
            var mi = sender as ToolStripMenuItem;
            if (mi == null) return;

            switch (mi.Text)
            {
                case "New":

                    break;
                case "Open":

                    break;
                case "Save":

                    break;
                case "SaveAs":

                    break;
                case "退出":
                    CloseApplication();
                    break;
            }
        }

        private void HelpMenuClick(object sender, EventArgs e)
        {
            var mi = sender as ToolStripMenuItem;
            if (mi == null) return;

            switch (mi.Text)
            {
                case "&Support":

                    break;
                case "关于":
                    var about = new AboutBox();
                    about.ShowDialog();
                    break;
            }
        }

        private void GvGroupViewItemClick(object sender, EventArgs e)
        {
            var bar = sender as GroupView;
            if (bar == null) return;
            if (bar.SelectedItem == -1) return;

            var parent = bar.Parent as GroupBar;
            if (parent == null) return;

            string title = parent.GroupBarItems[parent.SelectedItem].Text;
            string subtitle = bar.GroupViewItems[bar.SelectedItem].Text;
            tbFirst.Text = string.Format("{0} -> {1}", title, subtitle);

            tbFirst.Controls.Clear();
        }

        private void GbNavigatorClick(object sender, EventArgs e)
        {
            var bar = sender as GroupBar;
            if (bar == null) return;

            var barItem = bar.GroupBarItems[bar.SelectedItem];
            //tbFirst.Text = barItem.Text;

            var child = barItem.Client as GroupView;
            if (child != null)
                child.SelectedItem = -1;
        }

        private static void CloseApplication()
        {
            DialogResult result = MessageFactory.MessageBoxShow(Utilities.LogoutConfirmed);
            if (result == DialogResult.Yes)
                ApplicationExit.ExitApplication();
        }

        private void XpTaskBarBoxExitClick(object sender, EventArgs e)
        {
            CloseApplication();
        }

        private void GvMainGroupViewItemSelected(object sender, EventArgs e)
        {
            var bar = sender as GroupView;
            if (bar == null) return;

            if (bar.SelectedItem == -1)
                return;

            object tag = bar.GroupViewItems[bar.SelectedItem].Tag;
            if (tag != null)
            {
                switch (tag.ToString())
                {
                    case "1":   //  单人调动
                        InitializeControl(SingleControl);
                        tbFirst.Controls.Add(SingleControl);
                        break;
                    case "2":   //  整体划转 
                        InitializeControl(WholeControl);
                        tbFirst.Controls.Add(WholeControl);
                        break;
                    case "3":   // 数据修改
                        InitializeControl(ProfileControl);
                        tbFirst.Controls.Add(ProfileControl);
                        break;
                    case "4":   // 调动审批
                        var comp = new Approvals();
                        // 单人审批
                        InitializeControl(SingleApproval);
                        comp.tabPageSingle.Controls.Add(SingleApproval);
                        // 整体划转
                        InitializeControl(WholeApproval);
                        comp.tabPageWhole.Controls.Add(WholeApproval);

                        tbFirst.Controls.Add(comp);
                        comp.Dock = DockStyle.Fill;
                        break;
                    case "5":   // 数据报表
                        InitializeControl(TransferReport);
                        tbFirst.Controls.Add(TransferReport);
                        break;
                    default:
                        break;
                }
            }
        }

        private void InitializeControl(BaseControl control)
        {
            control.LogWriter = new LoggingWriter(LogMessage);
            control.InitializeControls();
            control.Dock = DockStyle.Fill;
        }

        private void GvAdminGroupViewItemSelected(object sender, EventArgs e)
        {
            var bar = sender as GroupView;
            if (bar == null) return;
            if (bar.SelectedItem == -1) return;

            object tag = bar.GroupViewItems[bar.SelectedItem].Tag;
            if (tag != null)
            {
                switch (tag.ToString())
                {
                    case "1":   //  主管部门 
                        InitializeControl(MUnitControl);
                        tbFirst.Controls.Add(MUnitControl);
                        break;
                    case "2":   //  单位管理
                        InitializeControl(SUnitControl);
                        tbFirst.Controls.Add(SUnitControl);
                        break;
                    case "3":
                        InitializeControl(UnitImporter);
                        tbFirst.Controls.Add(UnitImporter);
                        break;
                    case "4":   // 选项管理
                        InitializeControl(OptionControl);
                        tbFirst.Controls.Add(OptionControl);
                        break;
                    case "5":   // 数据报表
                        var comp = new Reports();
                        // 主管部门
                        InitializeControl(SuperReportControl);
                        comp.tabPageSuper.Controls.Add(SuperReportControl);
                        // 单位
                        InitializeControl(SubReportControl);
                        comp.tabPageSub.Controls.Add(SubReportControl);
                        // 图表
                        InitializeControl(PieChartControl);
                        comp.tabPageChart.Controls.Add(PieChartControl);

                        tbFirst.Controls.Add(comp);
                        comp.Dock = DockStyle.Fill;
                        break;
                    case "6":   // 模板
                        var templates = new Templates();
                        // 用于单位信息导入
                        InitializeControl(UnitTemplate);
                        templates.tabPageUnit.Controls.Add(UnitTemplate);
                        // 用于整体划转模板 
                        InitializeControl(WholeTemplate);
                        templates.tabPageWhole.Controls.Add(WholeTemplate);

                        tbFirst.Controls.Add(templates);
                        templates.Dock = DockStyle.Fill;
                        break;
                    case "7":   // 数据备份   
                        InitializeControl(SystemBackup);
                        tbFirst.Controls.Add(SystemBackup);
                        break;

                    default:
                        break;
                }
            }
        }

        private void LogMessage(string message)
        {
            var logItem = new LogItem { Date = DateTime.Now, Message = message };
            // send to UI
            lv_Message.Items.Add(logItem);
            // send to Log file
            logger.Log(LogLevel.Info, logItem);
        }

        private void ClToolStripMenuItemClick(object sender, EventArgs e)
        {
            lv_Message.Items.Clear();
        }

    }

    class LogItem
    {
        public DateTime Date { get; set; }
        public string Message { get; set; }

        public override string ToString()
        {
            return string.Format("{0} - {1}", Date, Message);
        }
    }

}
